package flyingpig.data.dao;
// default package
// Generated 2010-5-2 9:39:48 by Hibernate Tools 3.3.0.GA


import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;

import flyingpig.data.domain.ImageInfo;
import flyingpig.hibernate.HibernateUtil;

/**
 * Home object for domain model class ImageInfo.
 * @see .ImageInfo
 * @author Hibernate Tools
 */
public class ImageInfoHome {

	private static final Log log = LogFactory.getLog(ImageInfoHome.class);

	private final SessionFactory sessionFactory = getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return HibernateUtil.getSessionFactory();
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	public void persist(ImageInfo transientInstance) {
		log.debug("persisting ImageInfo instance");
		try {
			HibernateUtil.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(ImageInfo instance) {
		log.debug("attaching dirty ImageInfo instance");
		try {
			HibernateUtil.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ImageInfo instance) {
		log.debug("attaching clean ImageInfo instance");
		try {
			HibernateUtil.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(ImageInfo persistentInstance) {
		log.debug("deleting ImageInfo instance");
		try {
			HibernateUtil.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ImageInfo merge(ImageInfo detachedInstance) {
		log.debug("merging ImageInfo instance");
		try {
			ImageInfo result = (ImageInfo) HibernateUtil.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public ImageInfo findById(java.lang.Integer id) {
		log.debug("getting ImageInfo instance with id: " + id);
		try {
			ImageInfo instance = (ImageInfo) HibernateUtil.getCurrentSession()
					.get("flyingpig.data.domain.ImageInfo", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List findByURL(String url){
		try {
			Query q = HibernateUtil.getCurrentSession().createQuery(
					"from ImageInfo as S where S.url = :url");
			q.setString("url", url);
			return q.list();
		} catch (RuntimeException re) {
			log.error("find by url failed", re);
			throw re;
		}
	}

	/**
	 * 方法有问题
	 * @param instance
	 * @param maxResults
	 * @return
	 */
	public List findByExample(ImageInfo instance, int maxResults) {
		log.debug("finding ImageInfo instance by example");
		try {
			Criteria criteria = HibernateUtil.getCurrentSession().createCriteria(
			"flyingpig.data.domain.ImageInfo").add(Example.create(instance));
			
			criteria.setFirstResult(0);
			criteria.setMaxResults(maxResults);
			List results = criteria.list();
			
			log.info("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List findUnCachedImages(String concept, int maxResults){
		try {
			Query q = HibernateUtil.getCurrentSession().createQuery(
					"from ImageInfo as S where S.concept = :concept and S.cacheState = 'UNCACHED'");
			q.setString("concept", concept);
			q.setFirstResult(0);
			q.setMaxResults(maxResults);
			return q.list();
		} catch (RuntimeException re) {
			log.error("find by url failed", re);
			throw re;
		}
	}
}
